package com.longli.tank;

import com.longli.tank.chainofresponsibility.BulletTankCollider;
import com.longli.tank.chainofresponsibility.BulletWallCollider;
import com.longli.tank.chainofresponsibility.Collider;
import sun.awt.image.OffScreenImage;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Objects;

public class TankFrame extends Frame {
    public static final TankFrame INSTANCE=new TankFrame();

    public int GAME_WIDTH=800,GAME_HEIGHT=600;
    private Player myTank;
//    private List<Tank> tanks;
//    private List<Bullet> bullets;
//    private List<Explode> explodes;
    private List<AbstractGameObject> objects;

    private TankFrame(){
        this.setTitle("tank war");
        this.setLocation(400,100);
        this.setSize(GAME_WIDTH,GAME_HEIGHT);


        //Bullet bullet=new Bullet(100,100,Dir.D,Group.GOOD);
        this.addKeyListener(new TankKeyListener());
        initGameObject();

    }

    private void initGameObject() {
        myTank=new Player(100,100,Dir.D,Group.GOOD);

        objects=new ArrayList<>();
        int tankCount=Integer.parseInt(PropertyMgr.get("initTankCount"));
        for(int i=0;i<tankCount;i++){
            this.add(new Tank(100+i*50,200,Dir.D,Group.BAD));
        }

        this.add(new Wall(300,50,80,300));
    }


    public void add(AbstractGameObject go){
        //impl?
        objects.add(go);
    }

//    public void add(Bullet bullet){
//        bullets.add(bullet);
//    }

    Collider collider=new BulletTankCollider();
    Collider collider2=new BulletWallCollider();
    List<Collider> colliderList= Arrays.asList(new BulletTankCollider(),new BulletWallCollider());
    @Override
    public void paint(Graphics g){
        Color c = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("objects"+ objects.size(),10,50);
//        g.drawString("enemies"+tanks.size(),10,70);
//        g.drawString("explode"+explodes.size(),10,90);
        g.setColor(c);
        myTank.paint(g);
        for(int i=0;i<objects.size();i++){
            if(!objects.get(i).isLive()){
                objects.remove(i);
                break;
            }
            AbstractGameObject go1=objects.get(i);
            for(int j=0;j<objects.size();j++){
                AbstractGameObject go2=objects.get(j);
                for (Collider collider1 : colliderList) {
                    collider1.collide(go1,go2);
                }
            }
            objects.get(i).paint(g);
        }

//        for(int i=0;i<tanks.size();i++){
//            if(!tanks.get(i).isLive()){
//                tanks.remove(i);
//            }else{
//                tanks.get(i).paint(g);
//            }
//        }
//        for(int i=0;i<bullets.size();i++){
//            for(int j=0;j<tanks.size();j++){
//                bullets.get(i).collidesWithTank(tanks.get(j));
//            }
//            if(!bullets.get(i).isLive()){
//                bullets.remove(i);
//            }else{
//                bullets.get(i).paint(g);
//            }
//
//        }
//
//        for(int i=0;i<explodes.size();i++){
//            if(!explodes.get(i).isLive()){
//                explodes.remove(i);
//            }else{
//                explodes.get(i).paint(g);
//            }
//
//        }
    }
    Image offScreenImage=null;
    @Override
    public void update(Graphics g){
        if(offScreenImage==null){
            offScreenImage=this.createImage(GAME_WIDTH,GAME_HEIGHT);
        }
        Graphics goffScreen=offScreenImage.getGraphics();
        Color c = goffScreen.getColor();
        goffScreen.setColor(Color.BLACK);
        goffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
        goffScreen.setColor(c);
        paint(goffScreen);
        g.drawImage(offScreenImage,0,0,null);
    }

//    public void add(Explode explode) {
//        this.explodes.add(explode);
//    }

    public class TankKeyListener extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            myTank.keyPassed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            myTank.keyReleased(e);
        }
    }


}
